package com.rk.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rk.admin.domain.Comarticle;
import com.rk.admin.domain.Teacher;
import com.rk.admin.mapper.TeacherMapper;
import com.rk.admin.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 查询所有教师信息
     * @return
     */
    public List<Teacher> findallTeacher(){
        //条件查询
        LambdaQueryWrapper<Teacher> lqw=new LambdaQueryWrapper<Teacher>();
        lqw.select(Teacher::getId,Teacher::getName);
        List<Teacher> teachers = teacherMapper.selectList(lqw);
        return teachers;
    }

    /**
     * 通过id查询教师
     *
     * @param id
     * @return
     */
    @Override
    public Teacher findbyid(Integer id) {
        return teacherMapper.selectById(id);
    }

    /**
     * 通过id修改教师信息
     *
     * @param teacher
     * @return
     */
    @Override
    public Boolean updatebyid(Teacher teacher) {
        return teacherMapper.updatebyid(teacher)>0;
    }

    /**
     * 添加教师
     *
     * @param teacher
     * @return
     */
    @Override
    public Boolean add(Teacher teacher) {
        return teacherMapper.insert(teacher)>0;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Integer id) {
        return teacherMapper.deleteById(id)>0;
    }

    /**
     * 条件分页查询
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @Override
    public IPage<Teacher> selectPage(int page, int limit, String name) {
        IPage<Teacher> pageinfo=new Page<>(page,limit);
        //条件查询
        LambdaQueryWrapper<Teacher> lqw=new LambdaQueryWrapper<Teacher>();
        //根据id降序
        lqw.orderByDesc(Teacher::getId);


        //根据馆藏图书编号进行条件查询
        if(!"".equals(name)&&name!=null){
           lqw.like(Teacher::getName,name);
        }
        teacherMapper.selectPage(pageinfo,lqw);
        return pageinfo;
    }

}
